/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Entity.BeritaTA;
import Entity.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import library.Koneksi;

/**
 *
 * @author mr2
 */
public class BeritaTAModel {
    
    Connection koneksi;
    BeritaTA beritaTA;
    User user;
    
    public BeritaTAModel(){
        koneksi = Koneksi.getKoneksi();
    }
    
    public List<BeritaTA> ListBeritaTA(){
        
        PreparedStatement prepare = null;
        ResultSet result = null;
        List<BeritaTA> listBeritaTA = new ArrayList<BeritaTA>();

        
        try{
            String sql = "select * from berita_ta order by tanggal_posting desc";
            prepare = koneksi.prepareStatement(sql);
            
            result = prepare.executeQuery();
                       
            while(result.next()){
                
                BeritaTA beritaTA = new BeritaTA();
                User user = new User();
                user.setIdUser(result.getString("id_user"));
                
                beritaTA.setUser(user);
                beritaTA.setIdBerita(result.getInt("id_berita"));
                beritaTA.setJudulBerita(result.getString("judul_berita"));
                beritaTA.setIsiBerita(result.getString("isi_berita"));
                beritaTA.setTglPosting(result.getString("tanggal_posting"));
                listBeritaTA.add(beritaTA);
            }
            return listBeritaTA;
            
        }catch(SQLException ex){          
            System.out.println(ex.getMessage());
            return listBeritaTA;
        }finally{
            if(prepare != null){
                try{
                    prepare.close();
                }catch(SQLException ex){
                    System.out.println(ex.getMessage());
                }
            }
            if(result != null){
                try{
                    result.close();
                }catch(SQLException ex){
                    System.out.println(ex.getMessage());
                }
            }
        }
    }
    
    //Model insert pendaftaran sidang Proposal
    public boolean BuatBeritaTA(BeritaTA beritaTA){
        
        boolean hasil = true;
        PreparedStatement prepare = null;
        try{
            String sql = "INSERT INTO berita_ta(judul_berita, isi_berita, id_user) values(?,?,?)";
            
            prepare = koneksi.prepareStatement(sql);
            
            prepare.setString(1, beritaTA.getJudulBerita());
            prepare.setString(2, beritaTA.getIsiBerita());
            prepare.setString(3, beritaTA.getUser().getIdUser());
            
            int status = prepare.executeUpdate();
            if(status > 0){
                hasil = true;
            }else{
                hasil = false;
            }
            System.out.println(hasil);
            
        }catch(SQLException ex){
            hasil = false;
            System.out.println(ex.getMessage());
        }finally{
            if(prepare != null){
                try{
                    prepare.close();
                }catch(SQLException ex){
                    System.out.println(ex.getMessage());
                }
            }
        }
        return hasil;
    }
    
    public BeritaTA DetailBerita(BeritaTA beritaTA){
        PreparedStatement prepare = null;
        ResultSet result = null;
        
        try{
            String sql = "SELECT * from berita_ta where id_berita = ?";
            
            prepare = koneksi.prepareStatement(sql);

            prepare.setInt(1, beritaTA.getIdBerita());
            
            result = prepare.executeQuery();
            
            if(result.next()){
                beritaTA = new BeritaTA();
                User user = new User();
                
                user.setIdUser(result.getString("id_user"));
                beritaTA.setUser(user);
                beritaTA.setIdBerita(result.getInt("id_berita"));
                beritaTA.setJudulBerita(result.getString("judul_berita"));
                beritaTA.setIsiBerita(result.getString("isi_berita"));
                beritaTA.setTglPosting(result.getString("tanggal_posting"));
            }
            return beritaTA;
            
        }catch(SQLException ex){
            return beritaTA;
        }finally{
            if(prepare != null){
                try{
                    prepare.close();
                }catch(SQLException ex){
                    
                }
            }
            if(result != null){
                try{
                    result.close();
                }catch(SQLException ex){
                    
                }
            }
        
        }
    }
    
   //Model melakukan update bimbingan mahasiswa
   public boolean hapusBerita(BeritaTA beritaTA){
        PreparedStatement prepare = null;
        boolean hasil = true;
        try{            
            
            String sql = "delete from berita_ta where id_berita = ?";
            
            prepare = koneksi.prepareStatement(sql);
            
            prepare.setInt(1, beritaTA.getIdBerita());
            int status = prepare.executeUpdate();
            if(status > 0){
                hasil = true;
            }else{
                hasil = false;
            }
            
        }catch(SQLException ex){
            hasil = false;
            System.out.println(ex.getMessage());
        }finally{
            if(prepare != null){
                try{
                    prepare.close();
                }catch(SQLException ex){                   
                }
            }
        }
        
        return hasil;
   }
}
